package mapEditor;

import debug.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import mapEditor.map.ObjectTableModel;
import mapEditor.svg.SVGDOMTree;
import mapEditor.obj.DrawObject;

// ********************************************************************
// *** CLASS: TablePane
// ********************************************************************
public class TablePane extends JScrollPane
{ static final long serialVersionUID=0; // AD'HOC
  private SVGDOMTree domTree;
  private JTable table;
  private ObjectTableModel objectTableModel;

  // ******************************************************************
  // *** CONSTRUCTOR: TablePane
  // ******************************************************************
  public TablePane(SVGDOMTree domTree)
  { super();
    this.domTree=domTree;
    this.setViewportView(createTable());
    domTree.setTablePane(this);
  }

  public void setSelected(DrawObject selected)
  { int idx=objectTableModel.indexOf(selected);
	if(idx>=0) table.addRowSelectionInterval(idx,idx);	
  }
  
  // ******************************************************************
  // *** METHOD: updateTable
  // ******************************************************************
  public void updateTable()
  { this.setViewportView(createTable());
	MapEditor.repaint(); 
  }
  
  // ******************************************************************
  // *** METHOD: removeFromTable
  // ******************************************************************
  public void removeFromTable(DrawObject obj)
  { objectTableModel.remove(obj);
    updateTable(); 
  }

  // ******************************************************************
  // *** METHOD: createTable
  // ******************************************************************
  public JTable createTable()
  { objectTableModel=domTree.getObjectTableModel();
    table=new JTable(objectTableModel);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.addMouseListener(new TableMouseAdapter());
    table.setRowHeight(20);
    return(table);
  }
  
  class TableMouseAdapter extends MouseAdapter
  { public void mousePressed(MouseEvent e)
	{ int flg=e.getModifiers();
	  if((flg& InputEvent.BUTTON1_MASK)!=0)
	       mouseLeftPressed(e.getX(),e.getY());
	  else mouseRightPressed(e.getX(),e.getY());
	}
    private void mouseLeftPressed(int x, int y)
    { Debug.TRACE("MOUSE LEFT PRESSED: x="+x+", y="+y);
      int row=table.getSelectedRow();
      Debug.TRACE("MOUSE RIGHT PRESSED: SelectedRow="+row);
      if(row>=0) objectTableModel.select(row);
    }  
    private void mouseRightPressed(int x, int y)
    { Debug.TRACE("MOUSE RIGHT PRESSED: x="+x+", y="+y);
      int row=table.getSelectedRow();
      Debug.TRACE("MOUSE RIGHT PRESSED: SelectedRow="+row);
      if(row>=0) objectTableModel.edit(row);
    }  
  }

}